// Avatar 分类和类型定义
export type AvatarGroupType = 'PRIVATE' | 'PHOTO' | 'PUBLIC' | 'PUBLIC_PHOTO' | 'COMMUNITY_PHOTO';
export type AvatarBusinessType = 'user_upload' | 'generated' | 'public';
export type AvatarOwnership = 'user_owned' | 'public';

// 基础 Avatar Group 接口 (用于视频生成组件)
export interface AvatarGroup {
  group_id: string;
  name: string;
  num_looks: number;
  preview_image: string;
  group_type: AvatarGroupType;
  created_at?: number;
  account_id?: number;
}

// 单个 Avatar 接口 (用于视频生成组件)
export interface Avatar {
  avatar_id: string;
  name: string;
  preview_image: string;
  group_id?: string;
  account_id?: number;
  default_voice_id?: string; // Avatar的默认语音ID
}

// TalkingPhoto interfaces removed - not needed anymore

// 增强的 Avatar Group 接口
export interface EnhancedAvatarGroup {
  id: string;
  name: string;
  num_looks: number;
  preview_image: string;
  group_type: AvatarGroupType;
  created_at: number;
  default_voice_id?: string;
  train_status?: string;

  // 新增分类字段
  ownership: AvatarOwnership;
  business_type?: AvatarBusinessType;
  account_id?: number;
  is_public: boolean;
}

// Avatar 分类容器
export interface AvatarClassification {
  userOwned: {
    privateGroups: EnhancedAvatarGroup[]; // 用户私人创建的avatar组 (PRIVATE类型)
    photoGroups: EnhancedAvatarGroup[]; // 用户照片组 (PHOTO类型)
    allGroups: EnhancedAvatarGroup[]; // 所有用户组的组合
    totalGroups: number;
  };
  public: {
    officialGroups: EnhancedAvatarGroup[]; // HeyGen官方avatar组
    officialPhotos: EnhancedAvatarGroup[]; // HeyGen官方photo组
    communityPhotos: EnhancedAvatarGroup[]; // 社区共享avatar组
    totalGroups: number;
  };
}

// Avatar 过滤器配置
export interface AvatarFilter {
  ownership?: AvatarOwnership;
  groupType?: AvatarGroupType;
  searchTerm?: string;
  hasTraining?: boolean;
}

// Avatar 统计信息
export interface AvatarStats {
  totalUserGroups: number;
  totalPublicGroups: number;
  trainedGroups: number;
  pendingGroups: number;
}

// 分页配置
export interface PaginationConfig {
  currentPage: number;
  pageSize: number;
  totalItems: number;
  totalPages: number;
}

// Avatar 管理操作权限
export interface AvatarPermissions {
  canCreate: boolean;
  canEdit: boolean;
  canDelete: boolean;
  canTrain: boolean;
  canShare: boolean;
}

// 获取 Avatar 权限的工具函数
export const getAvatarPermissions = (
  avatarGroup: EnhancedAvatarGroup,
  isOwner: boolean
): AvatarPermissions => ({
  canCreate: isOwner,
  canEdit: avatarGroup.ownership === 'user_owned' && isOwner,
  canDelete: avatarGroup.ownership === 'user_owned' && isOwner,
  canTrain: avatarGroup.ownership === 'user_owned' && isOwner,
  canShare: avatarGroup.ownership === 'user_owned' && isOwner,
});

// Avatar 分类工具函数
export const classifyAvatarGroup = (group: Record<string, unknown>): EnhancedAvatarGroup => {
  let ownership: AvatarOwnership = 'public';

  // 基于 group_type 判断所有权
  if (group.group_type === 'PRIVATE' || group.group_type === 'PHOTO') {
    ownership = 'user_owned';
  }

  // 也可以基于 account_id 判断
  if (group.account_id != null) {
    ownership = 'user_owned';
  }

  return {
    id: (group.id as string) || (group.group_id as string) || '',
    name: (group.name as string) || '',
    num_looks: (group.num_looks as number) || 0,
    preview_image: (group.preview_image as string) || '',
    group_type: (group.group_type as AvatarGroupType) || 'PUBLIC',
    created_at: (group.created_at as number) || 0,
    default_voice_id: group.default_voice_id as string,
    train_status: group.train_status as string,
    ownership,
    business_type: group.business_type as AvatarBusinessType,
    account_id: group.account_id as number,
    is_public: ownership === 'public',
  };
};

// Avatar 分类排序函数
export const sortAvatarGroups = (groups: EnhancedAvatarGroup[]): EnhancedAvatarGroup[] => {
  return groups.sort((a, b) => {
    // 首先按所有权排序：用户拥有的在前
    if (a.ownership !== b.ownership) {
      return a.ownership === 'user_owned' ? -1 : 1;
    }

    // 然后按创建时间排序：最新的在前
    return b.created_at - a.created_at;
  });
};
